app.controller('statisticsController',function ($scope, $controller, $location, statisticsService, channelUserService,brandService,taxInfoService) {

    $controller('baseController',{$scope:$scope});

    $scope.searchEntity = {};



    //获取渠道下拉框集合
    $scope.findChannelUserNames = function () {

        //获取渠道id
        var channelUserId = $location.search()['channelUserId'];


        channelUserService.findChannelUserNames(channelUserId).success(function (response) {

            if (response.code == 200){

                $scope.channelUserNames = response.data;

            } else {
                alert(response.msg)
            }

        })

    };

    //获取渠道结算
    $scope.selectChannelStatistics = function () {

        console.log($scope.isClick);
        if ($scope.isClick){
            console.log("重复提交");
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        if (startTime == null){

            alert("选择开始时间");

            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }else {

            $scope.searchEntity.startTime = new Date(Date.parse(startTime.replace(/-/g, "/")));
        }

        if (endTime == null){

            alert("选择结束时间");

            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }else {
            $scope.searchEntity.endTime = new Date(Date.parse(endTime.replace(/-/g, "/")));

        }

        if ($scope.isNull($scope.searchEntity.channelUserId)){

            alert("未选择渠道");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        statisticsService.selectChannelStatistics($scope.searchEntity).success(function (response) {


            if (response.code == 200){

                $scope.channelStatisticsList = response.data;

                $scope.totalChannelCommission = response.totalChannelCommission;

                $scope.totalChannelCommissionTwo = response.totalChannelCommissionTwo;

            } else {
                alert(response.msg);
            }

            //还原按钮初始状态
            $scope.isClick = false;

        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;

        });

    };

    $scope.downloadParam = {data:null};

    $scope.downloadChannelStatistics = function () {

        if ($scope.isNull($scope.channelStatisticsList)) {

            alert("无渠道统计信息");
            return;
        }

        
        if (confirm("确认下载吗？")){
            console.log($scope.isClick);
            if ($scope.isClick){
                console.log("重复提交");
                return;
            }
            //标记为点击状态
            $scope.isClick = true;


            $scope.downloadParam.data = $scope.channelStatisticsList;

            statisticsService.downloadChannelStatistics($scope.downloadParam).success(function (response) {

                if (response.code == 200){

                    window.open(response.msg)

                } else {
                    alert(response.msg);
                }
                //还原按钮初始状态
                $scope.isClick = false;

            }).error(function () {
                //还原按钮初始状态
                $scope.isClick = false;

            });
        }

    };


    //重置查询参数
    $scope.resetSearchEntity = function () {

        $scope.searchEntity = {};

        $scope.channelStatisticsList = [];

        $scope.totalChannelCommission = 0;
    };


    //获取所有品牌
    $scope.findAllBrand = function () {

        brandService.findAll().success(function (response) {

            if (response.code == 200){

                $scope.brandList = response.data;

            }
        });
    };

    //获取所有税地明细
    $scope.findAllTaxInfo = function () {

        taxInfoService.findAll().success(function (response) {

            if (response.code == 200){
                $scope.taxInfoList = response.data;
            }

        });
    };

    //获取品牌结算
    $scope.selectBrandStatistics = function () {

        console.log($scope.isClick);
        if ($scope.isClick){
            console.log("重复提交");
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        var isError = false;

        if (startTime == null){

            alert("选择开始时间");

            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }else {

            $scope.searchEntity.startTime = new Date(Date.parse(startTime.replace(/-/g, "/")));
        }

        if (endTime == null){

            alert("选择结束时间");

            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }else {
            $scope.searchEntity.endTime = new Date(Date.parse(endTime.replace(/-/g, "/")));

        }

        var enCodeManagerStr = sessionStorage.getItem("MANAGER");
        //base64解码
        var managerStr = decodeURIComponent(window.atob(enCodeManagerStr));
        console.log(managerStr);
        var manager = JSON.parse(managerStr);

        let brandId = manager.brandId;

        if ($scope.isNull(brandId)) {
            if ($scope.isNull($scope.searchEntity.brandId) && $scope.isNull($scope.searchEntity.taxInfoName)) {
                alert("请选择品牌或税地");

                //还原按钮初始状态
                $scope.isClick = false;

                return;
            }
        } else {
            if ($scope.isNull($scope.searchEntity.brandId)){

                alert("未选择品牌");

                //还原按钮初始状态
                $scope.isClick = false;

                return;
            }
        }


        /*if (isError){
            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }*/

        statisticsService.selectBrandStatistics($scope.searchEntity).success(function (response) {


            if (response.code == 200){

                $scope.brandStatisticsList = response.data;

                $scope.totalBrandCommission = response.totalBrandCommission;

                $scope.totalBrandCommissionTwo = response.totalBrandCommissionTwo;

            } else {
                console.log(123456789)
                alert(response.msg);
            }

            //还原按钮初始状态
            $scope.isClick = false;

        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;

        });

    };


    //下载品牌统计
    $scope.downloadBrandStatistics = function () {

        if ($scope.brandStatisticsList == null || $scope.brandStatisticsList.length == 0) {

            alert("无品牌统计信息");
            return;
        }


        if (confirm("确认下载吗？")){
            console.log($scope.isClick);
            if ($scope.isClick){
                console.log("重复提交");
                return;
            }
            //标记为点击状态
            $scope.isClick = true;


            $scope.downloadParam.data = $scope.brandStatisticsList;

            statisticsService.downloadBrandStatistics($scope.downloadParam).success(function (response) {

                if (response.code == 200){

                    window.open(response.msg)

                } else {
                    alert(response.msg);
                }
                //还原按钮初始状态
                $scope.isClick = false;

            }).error(function () {
                //还原按钮初始状态
                $scope.isClick = false;

            });
        }
    };


    //获取渠道团队统计结算
    $scope.selectChannelOfflineStatistics = function () {

        console.log($scope.isClick);
        if ($scope.isClick){
            console.log("重复提交");
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        if (startTime == null){

            alert("选择开始时间");

            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }else {
            $scope.searchEntity.startTime = new Date(Date.parse(startTime.replace(/-/g, "/")));
        }

        if (endTime == null){

            alert("选择结束时间");
            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }else {
            $scope.searchEntity.endTime = new Date(Date.parse(endTime.replace(/-/g, "/")));
        }

        if ($scope.isNull($scope.searchEntity.channelUserId)){

            alert("未选择渠道");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        statisticsService.selectChannelOfflineStatistics($scope.searchEntity).success(function (response) {

            if (response.code == 200){

                $scope.channelOfflineStatisticsList = response.data;

                $scope.totalChannelOfflineCommission = response.totalChannelCommission;

                $scope.totalChannelCommissionForChild = response.totalChannelCommissionTwo;

            } else {
                alert(response.msg);
            }
            //还原按钮初始状态
            $scope.isClick = false;

        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;

        });

    };

    //渠道团队统计下载
    $scope.downloadChannelOfflineStatistics = function () {

        if ($scope.isNull($scope.channelOfflineStatisticsList)) {
            alert("无渠道团队统计信息");
            return;
        }


        if (confirm("确认下载吗？")){
            console.log($scope.isClick);
            if ($scope.isClick){
                console.log("重复提交");
                return;
            }
            //标记为点击状态
            $scope.isClick = true;


            $scope.downloadParam.data = $scope.channelOfflineStatisticsList;

            statisticsService.downloadChannelOfflineStatistics($scope.downloadParam).success(function (response) {

                if (response.code == 200){

                    window.open(response.msg)

                } else {
                    alert(response.msg);
                }
                //还原按钮初始状态
                $scope.isClick = false;

            }).error(function () {
                //还原按钮初始状态
                $scope.isClick = false;

            });
        }
    };

});